<?php

namespace app\weixin\controller;
use app\weixin\model\KnowledgeSubjectModel;
use think\Db;

//道具
class Prop extends Base{

    public function _initialize(){
        parent::_initialize();
    }

    public function index(){
        $map = [];
        $pagesize = config('paginate')['list_rows'];//每页数量
        $param=request()->param(); //获取url参数
        $lists = Db("prop")
            ->alias('p')
            ->join('knowledge_subject k','p.subject_id = k.id','left')
            ->where($map)
            ->field("p.id,p.name,p.logo,p.remark,p.type,p.action_type,p.reward_val,p.reward_scale,p.create_time,k.name as subject_name")
            ->paginate($pagesize,false,array('query' => array_splice($param,1)));
        $this->assign("lists",$lists);
        $this->assign("page", $lists->render());
        return $this->fetch();
    }

    //添加道具
    public function add_prop(){
        $KnowledgeSubjectModel = new KnowledgeSubjectModel();
        $subject = $KnowledgeSubjectModel->getAllSubject();
        $arr = $KnowledgeSubjectModel->subject($subject);
        if(request()->isPost()){
            $data = input('post.');
            $data['create_time'] = time();
            $res = db('prop')->insertGetId($data);
            return $this->success('操作成功');
        }
        $this->assign('list',$arr);
        return $this->fetch();
    }

    //修改道具
    public function edit_prop(){
        $KnowledgeSubjectModel = new KnowledgeSubjectModel();
        $subject = $KnowledgeSubjectModel->getAllSubject();
        $arr = $KnowledgeSubjectModel->subject($subject);
        $id = input('id');
        if(request()->isPost()){
            $data = input('post.');
            $res = db('prop')->where(['id'=>$id])->update($data);
            return $this->success('操作成功');
        }
        $data = db('prop')->where(['id'=>$id])->find();
        $this->assign('list',$arr);
        $this->assign('data',$data);
        return $this->fetch('add_prop');
    }

    //删除
    public function del_prop(){
        $id = input('id');
        $res = db('prop')->where(['id'=>$id])->delete();
        return $this->success('操作成功');
    }

} 